package com.song.service.impl;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.song.dao.MenuMapper;
import com.song.domain.Menu;
import com.song.domain.MenuVO;
import com.song.service.MenuService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.List;
@Service
public class MenuServiceImpl implements MenuService {

    @Autowired
    private MenuMapper menuMapper;
    /**
     * 查询全部的父子菜单信息
     *
     * @param pid
     */
    @Override
    public List<Menu> findSubMenuListByPid(int pid) {
        return menuMapper.findSubMenuListByPid(pid);
    }

    /**
     * 查询菜单列表
     */
    @Override
    public PageInfo findAllMenu(MenuVO menuVO) {
        PageHelper.startPage(menuVO.getCurrentPage(), menuVO.getPageSize());
        List<Menu> allMenu = menuMapper.findAllMenu();
        PageInfo<Menu> menuPageInfo = new PageInfo<>(allMenu);
        return menuPageInfo;
    }

    /**
     * 根据菜单ID 查询菜单信息(回显)
     *
     * @param id
     */
    @Override
    public Menu findMenuById(Integer id) {
        return menuMapper.findMenuById(id);
    }

    /**
     * 添加菜单
     *
     * @param menu
     */
    @Override
    public void saveMenu(Menu menu) {
        //补充信息
        Date date = new Date();
        menu.setCreatedTime(date);
        menu.setUpdatedTime(date);
        menu.setCreatedBy("system");
        menu.setUpdatedBy("system");

        menuMapper.saveMenu(menu);
    }

    /**
     * 修改菜单
     *
     * @param menu
     */
    @Override
    public void updateMenu(Menu menu) {
        //补全信息
        menu.setUpdatedTime(new Date());
        menu.setUpdatedBy("song");

        menuMapper.updateMenu(menu);
    }

    /**
     * 删除菜单
     *
     * @param id
     */
    @Override
    public void deleteMenu(Integer id) {
        menuMapper.deleteMenu(id);
    }
}
